package com.moteur.analysersite.validatorw3c;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import com.moteur.analysersite.util.TimeOut;

public class ValidateurHtml {
	private final String BASEURI = "http://validator.progysm.com/check.php?display=type&uri=http://";
	private String url;
	List<ErreurHtml> erreurHtml;
	Document doc;

	public ValidateurHtml(String url) {
		super();
		this.url = BASEURI + url;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public Document getDoc() {
		return doc;
	}

	public void setDoc(Document doc) {
		this.doc = doc;
	}

	public List<ErreurHtml> analsyer() {
		this.erreurHtml = new ArrayList<ErreurHtml>();
		TimeOut timeOutSecond = TimeOut.getInstance();
		Document doc;
		ErreurHtml obj;
		try {
			doc = Jsoup.connect(this.url)
					.timeout(timeOutSecond.getTimeout() * 1000).get();

			for (Element elmnt : doc.select("#errReport ul li")) {
				obj = new ErreurHtml();
				obj.setType("Type d'erreur: #"+elmnt.select(".partype").text().split("#")[1]);
				for (Element subElmnt : elmnt.select("p")) {
					obj.setLigne(subElmnt.select("a").text());
					obj.setDescription(subElmnt.text());
					this.erreurHtml.add(obj);
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return this.erreurHtml;
	}
}
